/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.fragments;

import java.util.Random;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.ViewGroup;

import pcgen.android.app.AppUtils;
import pcgen.android.app.TabContentFragment;
import pcgen.android.Logger;
import pcgen.android.app.DialogClickListenerHandler;

import pcgen.CharacterViewer.CharacterViewerActivity;
import pcgen.CharacterViewer.CharacterViewerApplication;
import pcgen.CharacterViewer.R;
import pcgen.CharacterViewer.resources.GameResource;

public abstract class AppTabContentFragment extends TabContentFragment
{
	public AppTabContentFragment()
	{
		_menuGroup = (new Random()).nextInt(10000000);
	}
	
	public AppTabContentFragment(String name)
	{
		super(name);
	}
	
	public AppTabContentFragment(GameResource resource)
	{
		super(resource.getName());
		
		_resource = resource;
	}
	
	public GameResource getResource()
	{
		return _resource;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
		try
		{
			super.onCreateOptionsMenu(menu, inflater);
			
			SubMenu characterMenu = menu.addSubMenu(getName());

			if (_menuShowRefresh)
				characterMenu.add(_menuGroup, MENU_REFRESH, 1, getString(R.string.menu_resource_refresh));
			if (_menuShowReload)
				characterMenu.add(_menuGroup, MENU_RELOAD, 1, getString(R.string.menu_resource_reload));
			if (_menuShowRemove)
				characterMenu.add(_menuGroup, MENU_REMOVE, 1, getString(R.string.menu_resource_remove));
		}
		catch (Throwable tr)
		{
			Logger.e(TAG, "onCreateOptionsMenu", tr);
		}
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	try
    	{
			switch (item.getItemId()) 
			{
				case MENU_REFRESH:
					refreshResource();
			    	return true;
				case MENU_RELOAD:
					AppUtils.confirm(getActivity(), getString(R.string.confirm_acceptance), 
				    		new DialogClickListenerHandler(getActivity()) 
				    		{
				                public void onClick(DialogInterface dialog, int whichButton) 
				                {
				                    if (whichButton != AppUtils.ID_ALERT_POSITIVE)
				                    	return;
				                    
				                    reloadResource();
				                }
				            });
			    	return true;
				case MENU_REMOVE:
					AppUtils.confirm(getActivity(), getString(R.string.confirm_acceptance), 
			    		new DialogClickListenerHandler(getActivity()) 
			    		{
			                public void onClick(DialogInterface dialog, int whichButton) 
			                {
			                    if (whichButton != AppUtils.ID_ALERT_POSITIVE)
			                    	return;
			                    
			                    removeResource();
			                }
			            });
			    	return true;
			}
    	}
		catch (Throwable tr)
		{
			Logger.e(TAG, "onOptionsItemSelected", tr);
		}
    	 
		return super.onContextItemSelected(item);
    }
	
    public CharacterViewerActivity getActivityEx()
	{
		return (CharacterViewerActivity)getActivity();
	}
	
	public CharacterViewerApplication getApplication()
	{
		return (CharacterViewerApplication)getActivity().getApplication();
	}
    
    @Override
	protected void initCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    	super.initCreate(inflater, container, savedInstanceState);
    	
    	setHasOptionsMenu(true);
    }
    
    protected void refreshResource()
    {
    }
	
    protected void reloadResource()
    {
    	
    }
    
    protected void removeResource()
    {
    }
    
	protected void setMenuShowRefresh(boolean value)
	{
		_menuShowRefresh = value;
	}
    
	protected void setMenuShowReload(boolean value)
	{
		_menuShowReload = value;
	}
	
	protected void setMenuShowRemove(boolean value)
	{
		_menuShowRemove = value;
	}
	
	protected void setResource(GameResource resource)
	{
		_resource = resource;
	}

	@Override
	protected void stateRestore(Bundle inState)
	{
		try
		{
			String name = inState.getString(KEY_GAME_RESOURCE);
			setResource(getApplication().getGameFolder().getResource(name));
			
			super.stateRestore(inState);
		}
    	catch(Throwable tr)
    	{
    		Logger.e(TAG, "stateRestore", tr);
    	}
	}
	
	@Override
	protected void stateSave(Bundle outState)
	{
		try
		{
			outState.putString(KEY_GAME_RESOURCE, _resource.getName());
			
			super.stateSave(outState);
		}
    	catch(Throwable tr)
    	{
    		Logger.e(TAG, "stateSave", tr);
    	}
	}

	private GameResource _resource;
	private int _menuGroup = 0;
	private boolean _menuShowRefresh;
	private boolean _menuShowReload;
	private boolean _menuShowRemove;
	
	private static final String KEY_GAME_RESOURCE = "KeyGameResource";

	private static final int MENU_REFRESH = 100001;
	private static final int MENU_RELOAD = 100002;
	private static final int MENU_REMOVE = 100003;

	private static final String TAG = AppTabContentFragment.class.getSimpleName();
}
